package org.aceor.mddal.gateway.datatype.impl;

import com.google.common.math.LongMath;
import org.aceor.mddal.gateway.datatype.DataType;
import org.aceor.mddal.gateway.datatype.cal.AbstractCalculator;
import org.aceor.mddal.gateway.datatype.cal.Calculator;

import java.math.RoundingMode;
import java.util.Date;

/**
 * Created by lxue on 16/4/16.
 */
public class DateType implements DataType<Date> {
    private Calculator calculator = new AbstractCalculator() {
        @Override
        public Object doAdd(Object value1, Object value2) {
            return LongMath.checkedAdd(((Date)value1).getTime(), ((Date)value2).getTime());
        }

        @Override
        public Object doSub(Object value1, Object value2) {
            return LongMath.checkedSubtract(((Date)value1).getTime(), ((Date)value2).getTime());
        }

        @Override
        public Object doMultiply(Object value1, Object value2) {
            return LongMath.checkedMultiply(((Date)value1).getTime(), ((Date)value2).getTime());
        }

        @Override
        public Object doDivide(Object value1, Object value2) {
            return LongMath.divide(((Date)value1).getTime(), ((Date)value2).getTime(), RoundingMode.DOWN);
        }
    };
    @Override
    public int compare(Date o1, Date o2) {
        long result = o1.getTime() - o2.getTime();
        if (result > 0) {
            return 1;
        } else if (result == 0) {
            return 0;
        } else {
            return -1;
        }
    }

    @Override
    public Calculator getCalculator() {
        return calculator;
    }
}
